package cn.sciento.farm.devicemanager.api.jsonrpc.impl;

import cn.sciento.farm.devicemanager.api.jsonrpc.CollectionJsonApi;
import cn.sciento.farm.devicemanager.common.constant.ResultConstant;
import cn.sciento.farm.devicemanager.common.util.DateUtil;
import cn.sciento.farm.devicemanager.common.util.ReturnUtil;
import cn.sciento.farm.devicemanager.dao.entity.DevCollection;
import cn.sciento.farm.devicemanager.dao.model.CollectionNode;
import cn.sciento.farm.devicemanager.dao.model.HistoryGetData;
import cn.sciento.farm.devicemanager.dao.model.HistoryGetForm;
import cn.sciento.farm.devicemanager.dao.model.ResponseMessage;
import cn.sciento.farm.devicemanager.service.CollectionService;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AutoJsonRpcServiceImpl
public class CollectionJsonApiImpl implements CollectionJsonApi {

    @Autowired
    private CollectionService collectionService;


    @Override
    public ResponseMessage update(DevCollection collection) {
        if(collection.getId() <= 0){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }else {
            DevCollection devCollection = new DevCollection();
            devCollection.setId(collection.getId());
            devCollection.setName(collection.getName());
            devCollection.setOnline(collection.getOnline());
            int count = collectionService.updateById(devCollection);
           return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,count,devCollection.getId());
        }
    }

    @Override
    public ResponseMessage get(Long id) {
        DevCollection devCollection = collectionService.getById(id);
        if(devCollection == null){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);

        }else {
            return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,devCollection);

        }
    }

    @Override
    public ResponseMessage delete(Long id) {
        int count = collectionService.deleteById(id);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,count);
    }

    @Override
    public ResponseMessage list(String eui, Integer landId) {
        if(landId <=0 && StringUtils.isEmpty(eui)){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }else {
            List<DevCollection> collectionList = new ArrayList<>();
            if(StringUtils.isEmpty(eui)){
                collectionList = collectionService.getByLandId(landId);
            }else {
                collectionList = collectionService.getByEui(eui);

            }
            return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,collectionList);
        }
    }

    @Override
    public ResponseMessage getMultiHistory(HistoryGetForm historyGetForm) {
        if(historyGetForm.getCollectionIds() == null
                || historyGetForm.getCollectionIds().size() == 0
        || historyGetForm.getBeginTime() == null
        || historyGetForm.getEndTime() == null){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        List<List<CollectionNode>> historyList = new ArrayList<>();
        for(Long collectionId : historyGetForm.getCollectionIds()){
            List<CollectionNode> collectionData = collectionService.getHistoryById(collectionId,
                    historyGetForm.getBeginTime(),historyGetForm.getEndTime());
            historyList.add(collectionData);
        }

        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,historyList);
    }

    @Override
    public ResponseMessage getByAreaId(Integer areaId) {
        List<DevCollection> collectionList = collectionService.getByAreaId(areaId);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,collectionList);
    }

    @Override
    public ResponseMessage listHistory(Long collectionId, Long time) {
        Long toDayZero = DateUtil.getTimeZeroHour(time).getTime();
        Long tomorrowZeroHour = DateUtil.getTimeTomorrowZeroHour(time).getTime();
        List<CollectionNode> collectionData = collectionService.getHistoryById(collectionId,toDayZero,tomorrowZeroHour);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,collectionData);
    }

    @Override
    public ResponseMessage getHistory(HistoryGetForm historyGetForm) {
        if(historyGetForm.getBeginTime()== null
        || historyGetForm.getEndTime() == null
        || historyGetForm.getDataType() == null
        || historyGetForm.getMinutes() == null){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        List<HistoryGetData> dataList = collectionService.getHistory(historyGetForm);
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,dataList);
    }

    @Override
    public ResponseMessage getMonthHistory(HistoryGetForm historyGetForm) {
        if(historyGetForm.getBeginTime()== null
                || historyGetForm.getEndTime() == null
                || historyGetForm.getCollectionIds().size() == 0
                ){
            return ReturnUtil.getReturnMap(ResultConstant.NULL_PARAMETER);
        }
        List<HistoryGetData> dataList = collectionService.getMonthHistory(historyGetForm);
        for(HistoryGetData data :dataList){
            DevCollection collection = collectionService.getById(data.getCollectionId());
            data.setCollection(collection);
        }
        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS,dataList);
    }
}
